﻿using Microsoft.AspNetCore.Mvc;
using LPP.Entity.ChatMessage;
using LPP.Service.ChatService;
using LPP.Entity.ChatMessageDto;

namespace Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class ChatController : ControllerBase
    {
        private readonly IChatService _chatService;

        public ChatController(IChatService chatService)
        {
            _chatService = chatService;
        }

        [HttpPost("send")]
        public async Task<IActionResult> SendMessage([FromBody] SendMessageRequest request)
        {
            try
            {
                var message = new ChatMessage
                {
                    SenderId = GetCurrentUserId(), // 需要实现获取当前用户ID的方法
                    ReceiverId = request.ReceiverId,
                    Content = request.Content
                };

                var result = await _chatService.SaveMessageAsync(message);
                if (result)
                    return Ok();
                return BadRequest("消息发送失败");
            }
            catch (Exception ex)
            {
                return BadRequest(ex.Message);
            }
        }

        [HttpGet("history")]
        public async Task<IActionResult> GetChatHistory([FromQuery] string userId, [FromQuery] string workerId)
        {
            try
            {
                var history = await _chatService.GetChatHistoryAsync(userId, workerId);
                return Ok(history);
            }
            catch (Exception ex)
            {
                return BadRequest(ex.Message);
            }
        }

        private int GetCurrentUserId()
        {
            // 从Token或Session中获取当前用户ID
            // 这里需要根据你的认证方式来实现
            throw new NotImplementedException();
        }
    }
}